﻿using System;
using System.Collections.Generic;
using System.Activities;
using System.ComponentModel;
using Microsoft.TeamFoundation.Build.Workflow.Design;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Build.Workflow;
using System.Security.Principal;

namespace TfsCloudDeploy.Workflow.Core.Activities
{
    /// <summary>
    /// Reflected from: Microsoft.TeamFoundation.Lab.Workflow.Activities.RunWorkflow.
    /// Assembly: Microsoft.TeamFoundation.Lab.Workflow.Activities, Version=10.0.0.0.
    /// </summary>
    [Designer(typeof(TeamBuildBaseActivityDesigner)), SRDescription(typeof(ExtendedWorkflowResources), "RunWorkFlowActivityDescription"), BuildActivity(HostEnvironmentOption.All)]
    public sealed class RunWorkflow : CodeActivity<int>
    {
        protected override int Execute(CodeActivityContext context)
        {
            if (string.IsNullOrEmpty(this.BuildDefinition.Get(context)))
            {
                throw new ArgumentException(ExtendedWorkflowResources.Get("IncompleteRunWorkflowActivity"));
            }
            string teamProject = this.ProjectName.Get(context);
            if (string.IsNullOrEmpty(teamProject))
            {
                teamProject = context.GetExtension<IBuildDetail>().TeamProject;
            }
            TfsTeamProjectCollection extension = context.GetExtension<TfsTeamProjectCollection>();
            IBuildServer server = extension.GetService<IBuildServer>();
            IBuildDefinition buildDefinition = server.GetBuildDefinition(teamProject, this.BuildDefinition.Get(context));
            IBuildRequest request = buildDefinition.CreateBuildRequest();

            // Build Controller:
            string buildController = this.BuildController.Get(context);
            if (!string.IsNullOrWhiteSpace(buildController))
            {
                request.BuildController = server.GetBuildController(buildController);
            }
            else
            {
                request.BuildController = buildDefinition.BuildController;
            }

            // Requested For:
            string requestedFor = this.RequestedFor.Get(context);
            if (!string.IsNullOrWhiteSpace(requestedFor))
            {
                request.RequestedFor = requestedFor;
            }

            // Priority:
            request.Priority = this.Priority.Get(context);

            // GetVersion:
            string customGetVersion = this.CustomGetVersion.Get(context);
            if (!string.IsNullOrWhiteSpace(customGetVersion))
            {
                request.GetOption = GetOption.Custom;
                request.CustomGetVersion = customGetVersion;
            }

            return server.QueueBuild(request, QueueOptions.None).Id;
        }

        [SRCategory(typeof(ExtendedWorkflowResources), "ActivityCategoryMiscellaneous"), SRDescription(typeof(ExtendedWorkflowResources), "BuildControllerDescription"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public InArgument<string> BuildController { get; set; }

        [SRCategory(typeof(ExtendedWorkflowResources), "ActivityCategoryMiscellaneous"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), SRDescription(typeof(ExtendedWorkflowResources), "BuildDefinitionDescription"), RequiredArgument]
        public InArgument<string> BuildDefinition { get; set; }

        [SRDescription(typeof(ExtendedWorkflowResources), "ProjectNameDescription"), SRCategory(typeof(ExtendedWorkflowResources), "ActivityCategoryMiscellaneous"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public InArgument<string> ProjectName { get; set; }

        [SRDescription(typeof(ExtendedWorkflowResources), "RequestedForDescription"), SRCategory(typeof(ExtendedWorkflowResources), "ActivityCategoryMiscellaneous"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public InArgument<string> RequestedFor { get; set; }

        [SRDescription(typeof(ExtendedWorkflowResources), "PriorityDescription"), SRCategory(typeof(ExtendedWorkflowResources), "ActivityCategoryMiscellaneous"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), DefaultValue(QueuePriority.Normal), RequiredArgument]
        public InArgument<QueuePriority> Priority { get; set; }

        [SRDescription(typeof(ExtendedWorkflowResources), "CustomGetVersionDescription"), SRCategory(typeof(ExtendedWorkflowResources), "ActivityCategoryMiscellaneous"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public InArgument<string> CustomGetVersion { get; set; }
    }
}
